package org.insia.smartposse.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.insia.smartposse.idao.IUserDAO;
import org.insia.smartposse.metier.Company;
import org.insia.smartposse.metier.Freelance;
import org.insia.smartposse.metier.User;
import org.insia.smartposse.utils.PasswordEncoder;
/**
 * DAO for User class.
 * @author sok hout
 *
 */
public class UserDAO extends GenericDAO<User, Long> implements IUserDAO {

	protected final Logger log = Logger.getLogger(getClass());
	/**
	 * Empty construtor.
	 *
	 */
	public UserDAO() {
		type = User.class;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.insia.smartposse.idao.IUserDAO#getUserByPasswordLogin(java.lang.String,
	 *      java.lang.String)
	 */
	public User getUserByPasswordLogin(String login, String password) {
		User usr = null;
		String crypt_pass = PasswordEncoder.encode(password);
		
		usr = (User) _SESSION.createQuery("from User as u where u.login like ? and password like ?").setString(0, login).setString(1, crypt_pass).uniqueResult();

		if (usr instanceof Freelance) {
			usr = (Freelance) usr;
		} else if (usr instanceof Company) {
			usr = (Company) usr;
		}
		return usr;
	}

	@SuppressWarnings("unchecked")
	public List<Freelance> getFromSkill(List<Long> id) {
		List<Freelance> freelance = new ArrayList<Freelance>();
		if (id.size() != 0)
			freelance = (List<Freelance>) _SESSION.createQuery("select distinct fs.freelance from FreelanceSkill as fs where fs.id in (:ids)").setParameterList("ids", id).list();
		return freelance;
	}
}
